package com.atguigu.srb.core.controller.api;

import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.common.utils.RegexValidateUtils;
import com.atguigu.srb.base.utils.JwtUtils;
import com.atguigu.srb.core.pojo.vo.LoginVO;
import com.atguigu.srb.core.pojo.vo.RegisterVO;
import com.atguigu.srb.core.pojo.vo.UserInfoVO;
import com.atguigu.srb.core.service.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lupengning
 * @version 1.0
 * @description:
 * @date 2021/6/8 17:16
 */
@Api(tags = "会员接口")
@RestController
@RequestMapping("/api/core/userInfo")
@Slf4j
//@CrossOrigin
public class ApiUserInfoController {

  @Resource
  private RedisTemplate redisTemplate;

  @Resource
  private UserInfoService userInfoService;

  @ApiOperation("会员注册")
  @PostMapping("/register")
  public R register(@RequestBody RegisterVO registerVO) {
    String mobile = registerVO.getMobile();
    String password = registerVO.getPassword();
    String code = registerVO.getCode();
    //校验手机号码
    Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
    Assert.isTrue(RegexValidateUtils.checkCellphone(mobile), ResponseEnum.MOBILE_ERROR);
    //校验密码和验证码
    Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
    Assert.notEmpty(code, ResponseEnum.CODE_NULL_ERROR);
    //验证码是否正确
    String codeGen = (String) redisTemplate.opsForValue().get("srb:sms:code:" + mobile);
    Assert.equals(code, codeGen, ResponseEnum.CODE_ERROR);

    userInfoService.register(registerVO);
    return R.ok().message("注册成功！");
  }

  @ApiOperation("会员登录")
  @PostMapping("/login")
  public R login(@RequestBody LoginVO loginVO, HttpServletRequest request) {
    String mobile = loginVO.getMobile();
    String password = loginVO.getPassword();

    Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
    Assert.notEmpty(password, ResponseEnum.MOBILE_NULL_ERROR);

    String ip = request.getRemoteAddr();
    UserInfoVO userInfoVO = userInfoService.login(loginVO, ip);
    return R.ok().data("userInfo", userInfoVO);
  }

  @ApiOperation("校验token")
  @GetMapping("/checkToken")
  public R checkToken(HttpServletRequest request) {

    String token = request.getHeader("token");
    boolean result = JwtUtils.checkToken(token);
    if (result) {
      return R.ok();
    } else {
      return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
    }
  }

  @ApiOperation("手机号码是否已经被注册")
  @GetMapping("/checkMobile/{mobile}")
  public boolean checkMobile(@PathVariable String mobile) {

    return userInfoService.isRegisterMobile(mobile);
  }
}
